const BASE_URL = 'http://192.168.1.42:8000/api/songs.php';

export default {
  // 获取歌曲列表
  getSongs(userId, playlist = '', page = 1, pageSize = 10) {
      return new Promise((resolve) => {
        let url = `${BASE_URL}?action=get_songs&user_id=${userId}&page=${page}&pageSize=${pageSize}`;
        if (playlist) url += `&playlist=${playlist}`;
        
        uni.request({
          url,
          success: (res) => resolve([null, res]),
          fail: (err) => resolve([err, null])
        });
      });
    },

  // 搜索歌曲
  searchSongs(userId, keyword, playlist = '') {
      return new Promise((resolve) => {
        let url = `${BASE_URL}?action=search_songs&user_id=${userId}&keyword=${encodeURIComponent(keyword)}`;
        if (playlist) url += `&playlist=${encodeURIComponent(playlist)}`;
        
        uni.request({
          url,
          success: (res) => resolve([null, res]),
          fail: (err) => resolve([err, null])
        });
      });
    },

  // 添加歌曲
  addSong(data) {
      return new Promise((resolve) => {
        uni.request({
          url: `${BASE_URL}?action=add_song`,
          method: 'POST',
          data,
          header: { 'Content-Type': 'application/json' },
          success: (res) => resolve([null, res]),
          fail: (err) => resolve([err, null])
        });
      });
    
  },

  // 更新歌曲
  updateSong(data) {
      return new Promise((resolve) => {
        uni.request({
          url: `${BASE_URL}?action=update_song`,
          method: 'PUT',
          data,
          header: { 'Content-Type': 'application/json' },
          success: (res) => resolve([null, res]),
          fail: (err) => resolve([err, null])
        });
      });
    },

  // 删除歌曲
  deleteSong(songId, userId) {
      return new Promise((resolve) => {
        uni.request({
          url: `${BASE_URL}?action=delete_song&id=${songId}&user_id=${userId}`,
          method: 'DELETE',
          success: (res) => resolve([null, res]),
          fail: (err) => resolve([err, null])
        });
      });
    },

  // 获取歌曲详情
  getSongDetail(songId, userId) {
      return new Promise((resolve) => {
        uni.request({
          url: `${BASE_URL}?action=song_detail&id=${songId}&user_id=${userId}`,
          success: (res) => resolve([null, res]), // 成功时返回 [null, response]
          fail: (err) => resolve([err, null])    // 失败时返回 [error, null]
        });
      });
  }
};